<?php

namespace api\modules\v1\forms\system;

use common\helpers\ValidatorHelper;
use common\models\system\SystemConfig;
use yii\base\Model;

/**
 * Class SystemConfigForm
 * @package api\modules\v1\forms\system
 */
class SystemConfigForm extends Model
{
    public $c_home_page_title;
    public $c_home_page_banner_type;
    public $c_home_page_banner_image;
    public $c_home_page_banner_video;
    public $c_home_page_banner_content;

    /**
     * @var int $kq_switch 考勤开关
     * @var string $kq_start_time 考勤开始时间
     * @var string $kq_end_time 考勤结束时间
     * @var string $kq_dingtalk_appkey 考勤钉钉appkey
     * @var string $kq_dingtalk_appsecret 考勤钉钉appsecret
     * @var string $kq_dingtalk_robot_token 钉钉机器人通知token
     * @var string $kq_dingtalk_robot_secret 钉钉机器人通知secret
     */
    /*public function __set($name, $value)
    {
        if (in_array($name, array_keys(SystemConfig::$names))) {
            $this->$name = $value;
        } else {
            parent::__set($name, $value); // TODO: Change the autogenerated stub
        }
    }*/

    /**
     * 获取首页形象图类型
     * @return int
     */
    public function getCHomePageBannerType(): int
    {
        return $this->{SystemConfig::NAME_C_HOME_PAGE_BANNER_TYPE};
    }

    public function rules()
    {
        return [
            // 参数验证
            [[SystemConfig::NAME_C_HOME_PAGE_TITLE, SystemConfig::NAME_C_HOME_PAGE_BANNER_TYPE, SystemConfig::NAME_C_HOME_PAGE_BANNER_CONTENT], 'required', 'message' => '{attribute}不能为空', 'on' => ['edit_wx_config']],

            [[SystemConfig::NAME_C_HOME_PAGE_TITLE], 'checkCHomePageTitle', 'skipOnEmpty' => false, 'on' => ['edit_wx_config']],
            [[SystemConfig::NAME_C_HOME_PAGE_BANNER_TYPE], 'checkCHomePageBannerType', 'skipOnEmpty' => false, 'on' => ['edit_wx_config']],
            [[SystemConfig::NAME_C_HOME_PAGE_BANNER_IMAGE], 'checkCHomePageBannerImage', 'skipOnEmpty' => false, 'on' => ['edit_wx_config']],
            [[SystemConfig::NAME_C_HOME_PAGE_BANNER_VIDEO], 'checkCHomePageBannerVideo', 'skipOnEmpty' => false, 'on' => ['edit_wx_config']],
            [[SystemConfig::NAME_C_HOME_PAGE_BANNER_CONTENT], 'checkCHomePageBannerContent', 'skipOnEmpty' => false, 'on' => ['edit_wx_config']],
        ];
    }

    public function scenarios()
    {
        $scenarios = parent::scenarios();

        // 编辑考勤配置
//        $scenarios['edit'] = array_values(array_diff(array_keys(SystemConfig::$names), SystemConfig::$wxMiniKeys));

        // 编辑钉钉机器人
        $scenarios['edit_wx_config'] = SystemConfig::$wxMiniKeys;

        return $scenarios;
    }

    /**
     * @return array
     */
    public function attributeLabels()
    {
        return SystemConfig::$names;
    }

    /**
     * 检验标题
     * @param $attribute
     * @return void
     */
    public function checkCHomePageTitle($attribute): void
    {
        if ($this->hasErrors()) {
            return;
        }

        if (mb_strlen($this->$attribute) > 32) {
            $this->addError($attribute, '标题过长【1-32字】');
            return;
        }
    }

    /**
     * 检验类型
     * @param $attribute
     * @return void
     */
    public function checkCHomePageBannerType($attribute): void
    {
        if ($this->hasErrors()) {
            return;
        }

        if (in_array($this->$attribute, [SystemConfig::NAME_C_HOME_PAGE_BANNER_TYPE_IMG, SystemConfig::NAME_C_HOME_PAGE_BANNER_TYPE_VIDEO]) === false) {
            $this->addError($attribute, '首页配置形象图类型错误');
            return;
        }
    }

    /**
     * 检验图片
     * @param $attribute
     * @return void
     */
    public function checkCHomePageBannerImage($attribute): void
    {
        if ($this->hasErrors()) {
            return;
        }
        if ($this->getCHomePageBannerType() != SystemConfig::NAME_C_HOME_PAGE_BANNER_TYPE_IMG) {
            return;
        }

        if (empty($this->$attribute)) {
            $this->addError($attribute, '图片不能为空');
            return;
        }
        if (count($this->$attribute) > 4) {
            $this->addError($attribute, '图片最多4张');
            return;
        }
    }

    /**
     * 检验视频
     * @param $attribute
     * @return void
     */
    public function checkCHomePageBannerVideo($attribute): void
    {
        if ($this->hasErrors()) {
            return;
        }
        if ($this->getCHomePageBannerType() != SystemConfig::NAME_C_HOME_PAGE_BANNER_TYPE_VIDEO) {
            return;
        }

        if (empty($this->$attribute)) {
            $this->addError($attribute, '视频不能为空');
            return;
        }
    }

    /**
     * 检验内容
     * @param $attribute
     * @return void
     */
    public function checkCHomePageBannerContent($attribute): void
    {
        if ($this->hasErrors()) {
            return;
        }
    }
}